package com.main.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.DeviceUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.main.dao.ChartsDAO;
import com.main.pojo.ChartsDetail;
import com.main.utils.CommonUtils;

@Controller
public class ChartsController {

	@Autowired
	private ChartsDAO chartsDao;
	
	public ChartsDAO getChartsDao() {
		return chartsDao;
	}

	public void setChartsDao(ChartsDAO chartsDao) {
		this.chartsDao = chartsDao;
	}
	
	@RequestMapping(value = "/charts/{type}", method = RequestMethod.GET)
	public @ResponseBody List<ChartsDetail> getCharts(@PathVariable String type){
		try{
			return chartsDao.getCharts(type,10);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value = "/bang-xep-hang", method = RequestMethod.GET)
	public String homeCharts(Model model, HttpServletRequest request){
		String path = "";
		try{
			model.addAttribute("chartsDetails", chartsDao.getCharts("WEEK",50));
			path = CommonUtils.getRootPathByDevice(DeviceUtils.getCurrentDevice(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path + "bxh";
	}
	
	@RequestMapping(value = "/bang-xep-hang/{type}", method = RequestMethod.GET)
	public @ResponseBody List<ChartsDetail> getFullCharts(@PathVariable String type){
		try{
			return chartsDao.getCharts(type,50);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
